from lxml import etree
import requests
import xlwt
import time


def get_res(url):
    headers={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    response=requests.get(url,headers=headers)
    return response.text

def get_info():
    format_url='https://www.qidian.com/all?page={}'
    # 前10页
    urls=[format_url.format(str(i)) for i in range(1,11)]
    for url in urls:
        selector=etree.HTML(get_res(url))
        infos=selector.xpath('//ul[@class="all-img-list cf"]/li')
        for info in infos:
            title=info.xpath('div[2]/h4/a/text()')[0]
            author=info.xpath('div[2]/p[1]/a[1]/text()')[0]
            style_1=info.xpath('div[2]/p[1]/a[2]/text()')[0]
            style_2=info.xpath('div[2]/p[1]/a[3]/text()')[0]
            style=style_1+'-'+style_2
            complete=info.xpath('div[2]/p[1]/span/text()')[0]
            introduce=info.xpath('div[2]/p[2]/text()')[0].strip()
            info_list=[title,author,style,complete,introduce]
            all_info_list.append(info_list)
        time.sleep(1)


if __name__=='__main__':
    all_info_list = []
    get_info()
    book = xlwt.Workbook(encoding='utf-8')
    sheet= book.add_sheet('Sheetl')
    header= ['title','author','style','complete','introduce']
    for h in range(len(header)):
        sheet.write(0, h, header[h])
    i=1
    for list in all_info_list:
        j=0
        for data in list:
            sheet.write(i, j, data)
            j+=1
        i+=1
    book.save('file/qidian.xls')